package com.sixteen.ordering_system.controller;


import com.sixteen.ordering_system.mapper.SysOrderMapper;
import com.sixteen.ordering_system.pojo.Dish;
import com.sixteen.ordering_system.pojo.DishCart;
import com.sixteen.ordering_system.pojo.SysOrder;
import com.sixteen.ordering_system.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sixteen
 * @since 2022-09-16
 */
@Controller
public class SysOrderController {
    @Autowired
    private SysOrderMapper sysOrderMapper;
    @RequestMapping("/addorder")
    public String order(HttpSession session, Dish dish,Integer dishId){
        SysOrder sysOrder = new SysOrder();
        dish.setDishId(dishId);
        sysOrder.setDish(dish);
        SysUser sysUser = (SysUser) session.getAttribute("loginUser");
        sysOrder.setSysUserId(sysUser.getSysUserId());
        sysOrder.setState(0);
        sysOrder.setRemark("abcd");
        sysOrder.setNumberOfDiners(1);
        sysOrderMapper.insertOrder(sysOrder);
        return "redirect:/ordering";
    }

    @RequestMapping("deleteOrder")
    public String delOrder(Integer dishId, HttpSession session,Dish dish){
        SysOrder sysOrder = new SysOrder();
        dish.setDishId(dishId);
        sysOrder.setDish(dish);
        SysUser sysUser = (SysUser) session.getAttribute("loginUser");
        sysOrder.setSysUserId(sysUser.getSysUserId());
        Integer integer = sysOrderMapper.delOrder(sysOrder);
        if (integer > 0){
            return "redirect:/ordering";
        }
        return "redirect:/erropage";
    }

    @RequestMapping("loadOrder")
    private String loadOrder(Integer sysOrderId,SysOrder sysOrder){
        sysOrderId = sysOrder.getSysOrderId();
        sysOrderMapper.updateState(sysOrderId);
        return "redirect:/ordering";
    }

    @RequestMapping("addorderNumber")
    public String addordernum(Integer numberOfDiners, Integer sysOrderId, SysOrder sysOrder){
        sysOrder.setNumberOfDiners(numberOfDiners);
        sysOrder.setSysOrderId(sysOrderId);
//        number++;
        Integer integer = sysOrderMapper.updateNum(numberOfDiners,sysOrderId);
        return "redirect:/ordering";
    }

    @RequestMapping("reduceorderNumber")
    public String reduceordernum(Integer numberOfDiners, Integer sysOrderId, SysOrder sysOrder){
        sysOrder.setNumberOfDiners(numberOfDiners);
        sysOrder.setSysOrderId(sysOrderId);
//        number++;
        Integer integer = sysOrderMapper.updateNum(numberOfDiners,sysOrderId);
        return "redirect:/shopping";
    }

}
